package com.cyy.hardwarecoding.ui.fragment;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.cyy.hardwarecoding.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YYCHEN on 2018/1/16.
 */

public class SensorFragment extends Fragment implements SensorEventListener{

    @BindView(R.id.tv_sensorInfo) TextView tv_sensorInfo;
    @BindView(R.id.tv_value) TextView tv_value;
    private Unbinder unBinder;
    private SensorManager sensorManager;
    private int sensorType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_sensor, container, false);
        unBinder = ButterKnife.bind(this, root);
        Bundle bundle = getArguments();
        if(bundle != null){
            sensorType = bundle.getInt("SensorType");
            Sensor sensor = sensorManager.getDefaultSensor(sensorType);
            showSensorInfo(sensor);
        }
        return root;
    }


    private void showSensorInfo(Sensor sensor){
        StringBuilder sb = new StringBuilder();
        sb.append("Sensor Name:"+sensor.getName());
        sb.append("\nSensor Type:"+sensor.getStringType());
        sb.append("\nMaximunRange:"+sensor.getMaximumRange());
        sb.append("\nVendor:"+sensor.getVendor());
        sb.append("\nVersion:"+sensor.getVersion());
        tv_sensorInfo.setText(sb.toString());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unBinder.unbind();
    }


    @Override
    public void onResume() {
        super.onResume();
        Sensor sensor = sensorManager.getDefaultSensor(sensorType);
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            Sensor sensor = sensorManager.getDefaultSensor(sensorType);
            sensorManager.unregisterListener(this, sensor);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Sensor sensor = sensorManager.getDefaultSensor(sensorType);
        sensorManager.unregisterListener(this, sensor);
    }

    /**
     * SensorEventListener
     * ***********************************************************/
    @Override
    public void onSensorChanged(SensorEvent event) {
        showSensorValue(event);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    private void showSensorValue(SensorEvent event){
        StringBuilder sb = new StringBuilder();
        sb.append("Accuracy:");
        switch (event.accuracy){
            case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                sb.append("SENSOR_STATUS_ACCURACY_HIGH");
                break;
            case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                sb.append("SENSOR_STATUS_ACCURACY_MEDIUM");
                break;
            case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                sb.append("SENSOR_STATUS_ACCURACY_LOW");
                break;
            case SensorManager.SENSOR_STATUS_UNRELIABLE:
                sb.append("SENSOR_STATUS_UNRELIABLE");
                break;
            case SensorManager.SENSOR_STATUS_NO_CONTACT:
                sb.append("SENSOR_STATUS_NO_CONTACT");
                break;
        }
        sb.append("\nUpdate Time:"+ TimeUtils.millis2String(event.timestamp/(1000*1000)));
        float[] values = event.values;
        for (int i=0; i < values.length; i++) {
            sb.append("\nValue["+i+"]:"+values[i]);
        }
        tv_value.setText(sb.toString());
    }
}
